﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreProvince;
using CoreBDS;

namespace BatDongSan.Modules
{
    public partial class Uc_SeachBusinessBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string _keyWord;

            int _fieldId;
            int _provinceId;
            int _wardId;
            try
            {
                try
                {
                    _keyWord = Request.QueryString["key"];
                }
                catch
                {
                    _keyWord = "";
                }

                _fieldId = Convert.ToInt32(Request.QueryString["f"]);
                _provinceId = Convert.ToInt32(Request.QueryString["tp"]);
                _wardId = Convert.ToInt32(Request.QueryString["quan"]);
                
                if (!IsPostBack)
                {
                    BindingDdlProvince(_provinceId);
                    BindingDdlWard(_provinceId, _wardId);
                    BindingDdlBusiness(_fieldId);
                }
            }
            catch { }
        }

        protected void BindingDdlProvince(int idProvince)
        {
            try
            {
                ddlProvince.Items.Clear();
                List<nlit_Province> list = ProvinceBll.GetList(true, Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        nlit_Province module = list[i];
                        ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                        ddlProvince.Items.Add(li);
                        if (list[i].Id == idProvince)
                        {
                            ddlProvince.SelectedIndex = i;
                        }
                    }
                }
                ListItem li1 = new ListItem("-- Chọn Tỉnh / Thành --", "0");
                ddlProvince.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingDdlWard(int idProvince, int idWard)
        {
            try
            {
                ddlWard.Items.Clear();
                if (idProvince > 0)
                {
                    List<nlit_Ward> list = WardBll.GetList(false, true, idProvince, Utility.strConnect);
                    if (list.Count > 0)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            nlit_Ward module = list[i];
                            ListItem li = new ListItem(list[i].Name, list[i].Id.ToString());
                            ddlWard.Items.Add(li);
                            if (list[i].Id == idWard)
                            {
                                ddlWard.SelectedIndex = i;
                            }
                        }

                    }
                }
                ListItem li1 = new ListItem("-- Chọn Quận / Huyện --", "0");
                ddlWard.Items.Insert(0, li1);
            }
            catch { }
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingDdlWard(Convert.ToInt32(ddlProvince.SelectedValue), 0);
        }

        protected void BindingDdlBusiness(int FieldId)
        {
            try
            {
                ddlBusiness.Items.Clear();

                List<Cvk_FieldOfBusiness> list = FieldOfBusinessBll.GetList(Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        Cvk_FieldOfBusiness module = list[i];
                        ListItem li = new ListItem(list[i].Name, list[i].FieldId.ToString());
                        ddlBusiness.Items.Add(li);
                        if (list[i].FieldId == FieldId)
                        {
                            ddlBusiness.SelectedIndex = i;
                        }
                    }

                }
                ListItem li1 = new ListItem("-- Chọn lĩnh vực --", "0");
                ddlBusiness.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            string re = "";
            re += "?f=" + ddlBusiness.SelectedValue;
            re += "&tp=" + ddlProvince.SelectedValue;
            re += "&quan=" + ddlWard.SelectedValue;
            if (!string.IsNullOrEmpty(txtTuKhoa.Text.Trim()))
            {
                re += "&key=" + txtTuKhoa.Text.Trim();
            }
            string link = "/SearchBusiness.aspx" + re;
            Response.Redirect(link);
        }
    }
}